package com.cslg.ac.demo.service.host;

import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.dao.host.HostDAO;
import com.cslg.ac.demo.dao.host.HostFosterDAO;
import com.cslg.ac.demo.dao.host.po.HostFosterPO;
import com.cslg.ac.demo.dao.host.po.HostPO;
import com.cslg.ac.demo.dao.pet.PetCommonDAO;
import com.cslg.ac.demo.dao.pet.PetFosterDAO;
import com.cslg.ac.demo.dao.pet.UserPetDAO;
import com.cslg.ac.demo.dao.pet.po.PetFosterPO;
import com.cslg.ac.demo.dao.post.HostEvaluationDAO;
import com.cslg.ac.demo.dao.user.person.PersonInfoDAO;
import com.cslg.ac.demo.entity.host.Host;
import com.cslg.ac.demo.entity.host.HostFoster;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;


@Service
@PropertySource(value = {"classpath:host.properties"},encoding="utf-8")
public class HostService {
    @Autowired
    HostDAO hostDAO;
    @Autowired
    PersonInfoDAO personInfoDAO;
    @Autowired
    HostFosterDAO hostFosterDAO;
    @Autowired
    PetFosterDAO petFosterDAO;
    @Autowired
    UserPetDAO userPetDAO;
    @Autowired
    PetCommonDAO petCommonDAO;

    @Value("${hostBuket}")
    String hostBuket;
    @Value("${fosterType}")
    int fosterType;


    public Integer[] sortHost(Double latitude, Double longitude,
                             int offset, int num,
                             String address, Integer credit){
        if(address != null && credit != null && latitude != null && longitude != null){
            return hostDAO.sortByThree(longitude, latitude, address, offset, num);
        }
        if(address != null && longitude != null && latitude != null){
            return hostDAO.sortByAddressAndDistance(longitude, latitude, address, offset, num);
        }
        if(address != null && credit != null){
            return hostDAO.sortByAddressAndCredit(address, offset, num);
        }
        if(longitude != null && latitude != null && credit != null){
            return hostDAO.sortByLatAndLongAndCredit(longitude, latitude, offset, num);
        }
        if(address != null ){
            return hostDAO.selectIdByAddress(address, offset, num);
        }
        if(credit != null){
            return hostDAO.sortByCredit(offset, num);
        }
        if(latitude != null && longitude != null){
            return hostDAO.sortByLatAndLong(longitude, latitude, offset, num);
        }

        return null;
    }




    public HostFoster getHostFosterById(int fosterId,int masterId){
        Integer getUser = userPetDAO.selectUserByPet(fosterId);
        if(getUser == null || getUser != masterId){
            return null;
        }
        HostFosterPO hostFosterPO = hostFosterDAO.selectByFosterId(fosterId);
        if(hostFosterPO == null){
            return null;
        }
        HostFoster hostFoster = new HostFoster();
        BeanUtils.copyProperties(hostFosterPO,hostFoster);
        return hostFoster;
    }


    public int updateState(int fosterId, int userId, int state){
        Integer getUser = userPetDAO.selectUserByPet(fosterId);
        if(getUser == null || getUser != userId){
            return ResponseCode.PERMISSION_FORBIDDEN;
        }
        HostFosterPO hostFosterPO = hostFosterDAO.selectByFosterId(fosterId);
        if(hostFosterPO == null){
            return ResponseCode.USER_NOT_EXISTS;
        }
        if(state == 1){
            hostFosterPO.setFosterSend(1);
            hostFosterPO.setFinish(0);
        }else if(state == 2){
            hostFosterDAO.delete(fosterId);
            return ResponseCode.REQUEST_SUCCESSED;
        }else {
            return ResponseCode.PARAM_ILLEGAL;
        }
        hostFosterDAO.updateByFosterId(hostFosterPO);
        return ResponseCode.REQUEST_SUCCESSED;
    }



    public int updateInfo(String beginTime, String endTime, int fosterId,int userId, int newFosterId){
        Integer getUser = userPetDAO.selectUserByPet(fosterId);
        Integer getNewUser = userPetDAO.selectUserByPet(newFosterId);
        boolean flag = (getUser == null || getUser != userId) || (newFosterId != 0 && (getNewUser == null || getNewUser != userId));
        if(flag){
            return ResponseCode.PERMISSION_FORBIDDEN;
        }
        HostFosterPO hostFosterPO = hostFosterDAO.selectByFosterId(fosterId);
        if(hostFosterPO == null){
            return ResponseCode.USER_NOT_EXISTS;
        }
        if(newFosterId != 0){
            hostFosterPO.setFosterId(newFosterId);
        }
        hostFosterPO.setBeginTime(beginTime);
        hostFosterPO.setEndTime(endTime);
        hostFosterDAO.updateById(hostFosterPO);
        return ResponseCode.REQUEST_SUCCESSED;
    }

    public int fostersPet(HostFoster hostFoster){
        HostPO hostPO = hostDAO.selectById(hostFoster.getHostId());
        PetFosterPO petFosterPO = petFosterDAO.selectById(hostFoster.getFosterId());
        HostFosterPO getFoster = hostFosterDAO.selectByFosterId(hostFoster.getFosterId());
        if(hostPO == null || petFosterPO == null){
            return ResponseCode.USER_NOT_EXISTS;
        }
        if(getFoster != null && getFoster.getFosterBack() != 1){
            return ResponseCode.USER_ALREADY_EXISTS;
        }
        HostFosterPO hostFosterPO = new HostFosterPO();
        BeanUtils.copyProperties(hostFoster, hostFosterPO);
        hostFosterDAO.insert(hostFosterPO);
        return ResponseCode.REQUEST_SUCCESSED;
    }

    public Host getInfo(int id){
        HostPO hostPO = hostDAO.selectById(id);
        if(hostPO == null){
            return null;
        }
        Host host = new Host();
        BeanUtils.copyProperties(hostPO,host);
        return host;
    }


    public int beHost(Host host){
        HostPO hostPO = new HostPO();
        BeanUtils.copyProperties(host,hostPO);
        if(hostDAO.selectById(host.getId()) != null){
            return ResponseCode.USER_ALREADY_EXISTS;
        }
        hostDAO.insert(hostPO);
        return ResponseCode.REQUEST_SUCCESSED;
    }
}
